package com.sharedlease.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharedlease.entity.Shop_car;
import com.sharedlease.entity.R;
import com.sharedlease.service.Shop_carService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-03-03
 */
@RestController
@Api(tags = "购物车相关接口")
@RequestMapping("/shop_car")
public class Shop_carController {
    @Autowired
    public Shop_carService shop_carService;
    @GetMapping("/getById/{id}")
    @ApiOperation("根据一个id查询购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的购物车表的id", defaultValue = "1"),

    }
    )
    public Shop_car getById(@PathVariable Long id){
        Shop_car shop_car = shop_carService.getById(id);
        return shop_car;
    }
    @GetMapping("/getByUid/{uid}")
    @ApiOperation("根据一个uid查询购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "要查询的购物车表的uid", defaultValue = "1"),

    }
    )
    public List<Shop_car> getByUid(@PathVariable Long uid){
        QueryWrapper<Shop_car> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid+"");
        List<Shop_car> shop_cars = shop_carService.list(wrapper);
        return shop_cars;
    }
    @GetMapping("/list")
    @ApiOperation("查询全部购物车表记录")
    public List<Shop_car> list(){
        List<Shop_car> list = shop_carService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一个购物车表记录")
    public R save(@RequestBody Shop_car shop_car) {

        return R.operate(shop_carService.save(shop_car));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改一条购物车表记录")
    R update(@RequestBody Shop_car shop_car){
        if(shop_carService.updateById(shop_car)){
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除购物车表记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的购物车id", defaultValue = "-1"),

    }
    )
    R removeById(@PathVariable Long id) {

        return R.operate (shop_carService.removeById(id));
    }

}

